const commodity = require('../../services/commodity.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputText: '',
        searchHistory: [],
        down: false, // 显示加载更多
        list: [],
        pageNum: 1,
        pageSize: 10,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const his = wx.getStorageSync("his")
        this.setData({
            searchHistory: his == null ? [] : his
        })
    },

    /**
     * 获取输入的值
     */
    getText: function (e) {
        this.setData({
            inputText: e.detail.value
        })
    },

    /**
     * 获取到焦点后 清除热门内容
     */
    clearText: function (e) {
        this.setData({
            input_value: '',
            inputText: ''
        })
    },

    /**
     * 跳转到书籍详情
     * @param e
     */
    toDetail: function (e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../detail/detail?id=' + id
        })
    },

    /**
     * 搜索
     */
    search: function () {
        const val = this.data.inputText
        if (val === '' || val === undefined) {
            wx.showModal({
                title: '提示',
                content: '请先输入要搜索的内容',
                showCancel: false
            })
            return;
        }
        wx.showLoading({
            title: '加载中',
        })
        let data = {
            searchVal: this.data.inputText,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        }
        this.cacheHist(this.data.inputText)
        this.setData({
            list: []
        })
        this.findList(data)
    },

    cacheHist(val) {
        const his = wx.getStorageSync("his") === '' ? [] : wx.getStorageSync("his")
        let save = true;
        if (his.length > 0) {
            for (let i in his) {
                if (his[i] === val) {
                    save = false
                    break
                }
            }
        }
        if (save) {
            his.push(val)
            wx.setStorageSync("his", his)
            this.setData({
                searchHistory: his
            })
        }
    },

    deleteHis() {
        const that = this
        wx.removeStorage({
            key: 'his',
            success(res) {
                that.setData({
                    searchHistory: []
                })
            }
        })
    },

    searchHis(e) {
        wx.showLoading({
            title: '加载中',
        })
        const val = e.currentTarget.dataset.val
        this.setData({
            list: [],
            pageNum: 1,
            down: false
        })
        const data = {
            searchVal: val,
            pageNum: 1,
            pageSize: this.data.pageSize
        }
        this.findList(data)
    },

    /**
     * 上拉触底时触发
     */
    bindscrolltolower: function () {
        wx.showLoading({
            title: '加载中',
        })
        if (this.data.down) {
            this.setData({
                down: false
            })
            const data = {
                searchVal: this.data.inputText,
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize
            }
            this.findList(data)
        }
    },

    findList(data) {
        const that = this
        commodity.search(data, (res) => {
            if (res.code === 0) {
                let l = that.data.list
                let newRes = l.concat(res.data.data)
                that.setData({
                    list: newRes,
                    pageNum: that.data.pageNum + 1
                })
                // 可下拉
                this.setData({
                    down: res.data.isHasNextPage
                })
            }
            wx.hideLoading()
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})